package com.moinut.studentattendancemanager.view.comment;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.PasswordController;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author MOILING
 */
public class ChangePasswordFrame extends BaseFrame<ChangePasswordFrame.ChangePasswordPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;

    public ChangePasswordFrame() {
        super("修改密码", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected ChangePasswordPanel onSetContentPane() {
        return new ChangePasswordPanel();
    }

    /* package */ class ChangePasswordPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JLabel mCurrentPasswordLabel, mNewPasswordLabel, mConfirmPasswordLabel;
        private JPasswordField mCurrentPasswordField, mNewPasswordField, mConfirmPasswordField;
        private JButton mOkButton, mBackButton;
        private JPanel mPasswordPanel, mButtonPanel;

        /*package*/ ChangePasswordPanel() {
            setLayout(new BorderLayout());
            initPasswordPanel();
            initButtonPanel();
        }

        private void initButtonPanel() {
            mOkButton = new JButton("确认修改");
            mBackButton = new JButton("返回上级菜单");
            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mButtonPanel.add(mOkButton);
            mButtonPanel.add(mBackButton);
            add(mButtonPanel, BorderLayout.SOUTH);

            mBackButton.addActionListener(this);
            mOkButton.addActionListener(this);
        }

        private void initPasswordPanel() {
            mCurrentPasswordLabel = new JLabel("当前密码");
            mNewPasswordLabel = new JLabel("     新密码");
            mConfirmPasswordLabel = new JLabel("确认密码");
            mCurrentPasswordField = new JPasswordField(18);
            mNewPasswordField = new JPasswordField(18);
            mConfirmPasswordField = new JPasswordField(18);

            mPasswordPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mPasswordPanel.setBorder(new TitledBorder(new LineBorder(Color.GRAY), "修改密码"));

            mPasswordPanel.add(mCurrentPasswordLabel);
            mPasswordPanel.add(mCurrentPasswordField);
            mPasswordPanel.add(mNewPasswordLabel);
            mPasswordPanel.add(mNewPasswordField);
            mPasswordPanel.add(mConfirmPasswordLabel);
            mPasswordPanel.add(mConfirmPasswordField);

            add(mPasswordPanel, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(ChangePasswordFrame.this);
            } else if (e.getSource().equals(mOkButton)) {
                PasswordController.updatePassword(UserManager.userType, UserManager.userId,
                        new String(mCurrentPasswordField.getPassword()),
                        new String(mNewPasswordField.getPassword()),
                        new String(mConfirmPasswordField.getPassword()),
                        new OnUpdateListener() {
                            @Override
                            public void onSuccess(String success) {
                                JOptionPane.showMessageDialog(null, success, "成功", JOptionPane.INFORMATION_MESSAGE);
                            }

                            @Override
                            public void onError(String error) {
                                JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                            }
                        });
            }
        }
    }
}
